package cn.wolfcode.controller;


import cn.wolfcode.domain.ExamQuestion;
import cn.wolfcode.domain.Question;
import cn.wolfcode.domain.QuestionItem;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IExamQuestionService;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.service.IQuestionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/examQuestion")
public class ExamQuestionController {
 @Autowired
    private    IExamService examService;

    @Autowired
    private IExamQuestionService examQuestionService;
    @Autowired
    private IQuestionService questionService;

    @RequiredPermission(name="试卷题目页面",expression="examQuestion:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<ExamQuestion> pageInfo = examQuestionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "examQuestion/list";
    }


  /*  @RequiredPermission(name="试卷题目删除",expression="examQuestion:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           examQuestionService.delete(id);
        }
        return "redirect:/examQuestion/list";
    }*/


    @RequiredPermission(name="试卷题目新增/编辑",expression="examQuestion:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate( ExamQuestion examQuestion, Model model, HttpSession session) {
        if (examQuestion.getExam()!=null){

            examService.save(examQuestion.getExam());
        }

        session.setAttribute("ex",examQuestion);
        int sequence=1;
        List<Question> questions = questionService.selectForListByType(1);
        Collections.shuffle(questions);
        for (int i= 0; i<examQuestion.getOneCount() ; i++){

            Question question = questions.get(i);
            ExamQuestion examQuestion1 = new ExamQuestion();
            examQuestion1.setExam(examQuestion.getExam());
            examQuestion1.setQuestion(question);
            examQuestion1.setSequence(sequence);
            examQuestion1.setScore(examQuestion.getOneScore());
            examQuestionService.save(examQuestion1);
            sequence++;
        }
        List<Question> questions2 = questionService.selectForListByType(2);
        Collections.shuffle(questions2);
        for (int i= 0; i<examQuestion.getTwoCount() ; i++){

            Question question = questions2.get(i);
            ExamQuestion examQuestion1 = new ExamQuestion();
            examQuestion1.setExam(examQuestion.getExam());
            examQuestion1.setQuestion(question);
            examQuestion1.setSequence(sequence);
            examQuestion1.setScore(examQuestion.getTwoScore());
            examQuestionService.save(examQuestion1);
            sequence++;
        }
        List<Question> questions3 = questionService.selectForListByType(3);
        Collections.shuffle(questions3);
        for (int i= 0; i<examQuestion.getThreeCount() ; i++){

            Question question = questions3.get(i);
            ExamQuestion examQuestion1 = new ExamQuestion();
            examQuestion1.setExam(examQuestion.getExam());
            examQuestion1.setQuestion(question);
            examQuestion1.setSequence(sequence);
            examQuestion1.setScore(examQuestion.getThreeScore());
            examQuestionService.save(examQuestion1);
            sequence++;
        }


        /*if (examQuestion.getId() == null) {
            examQuestionService.save(examQuestion);
        } else {
            examQuestionService.update(examQuestion);
        }*/
        return "forward:/examQuestion/show";
    }


    @RequiredPermission(name="试卷题目新增/编辑",expression="examQuestion:saveOrUpdate")
    @RequestMapping("/input")
    public String input( Model model) {
   /*     if (id != null) {
            model.addAttribute("examQuestion",examQuestionService.get(id));
        }*/
        return "examQuestion/input";
    }
@RequestMapping("/show")
    public String show(HttpServletRequest request,Model model){

    ExamQuestion examQuestion = (ExamQuestion)request.getSession().getAttribute("ex");
    List<Question> questions = questionService.selectByExamId(examQuestion.getExam().getId(),1);
    for (int i=0;i<questions.size();i++){
        List<QuestionItem> questionItems = questionService.queryByQuesId(questions.get(i).getId());
        questions.get(i).setItems(questionItems);
    }
   model.addAttribute("questions",questions);

    List<Question> questions2 = questionService.selectByExamId(examQuestion.getExam().getId(),2);
    for (int i=0;i<questions2.size();i++){
        List<QuestionItem> questionItems = questionService.queryByQuesId(questions2.get(i).getId());
        questions2.get(i).setItems(questionItems);
    }
    model.addAttribute("questions2",questions2);

    List<Question> questions3 = questionService.selectByExamId(examQuestion.getExam().getId(),3);
    for (int i=0;i<questions3.size();i++){
        List<QuestionItem> questionItems = questionService.queryByQuesId(questions3.get(i).getId());
        questions3.get(i).setItems(questionItems);
    }
    model.addAttribute("questions3",questions3);

    return "test/试卷预览页面";
    }
}
